<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\index\model;

use think\Model;

/**
 * 角色模型
 * @package app\admin\model
 */
class News extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'admin_news';

    public function getLists($page=1){
        $fields=['create_time','title_en','cover_image','id','sub_title'];
        $data=self::where(['status'=>1])->field($fields)->order('create_time','desc')->page($page,6)->select()->toArray();
        if($data){
            //格式化数据
            foreach ($data as &$val){
                $val['create_time']=$this->formatDate($val['create_time']);
            }
        }
        $list['data']=$data;
        $list['count']=$this->getListCount();
        $list['cur_page']=$page;
        $list['pages']=ceil($list['count']/6);
        return $list;
    }

    public function getAllLists(){
        $fields=['create_time','title_en','cover_image','id','sub_title'];
        $data=self::where(['status'=>1])->field($fields)->order('create_time','desc')->select()->toArray();
        if($data){
            //格式化数据
            foreach ($data as &$val){
                $val['create_time']=date('m.d.Y',strtotime($val['create_time']));
            }
        }

        return $data;
    }

    public function getList($id){
        $data=self::field(['cover_image','title_en','content_en','create_time'])->get($id);
        $data['create_time']=$this->formatDate($data['create_time']);
        return $data;
    }

    private function getListCount(){
        return self::where(['status'=>1])->count();
    }

    private function formatDate($time){
        $month=date('m',strtotime($time));
        $month=config('setting.month_en.'.$month);
        return $month.' '.date('Y.m',strtotime($time));
    }

}
